package com.eleadmin.common.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;

import com.eleadmin.common.system.entity.MedicalImaging;


import com.eleadmin.common.system.param.SaleDataParam;
import com.eleadmin.common.system.service.MedicalImagingService;
import com.eleadmin.common.system.vo.PayNumListVO;
import com.eleadmin.common.system.vo.SaleDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Objects;


/**
 * 统计数据控制器
 */
@Api(tags = "统计数据管理")
@RestController
@RequestMapping("/system/analysis")
public class AnalysisController extends BaseController {
    @Resource
    private MedicalImagingService medicalImagingService;

    @OperationLog
    @ApiOperation("销售量及茶叶销售额排行查询")
    @PostMapping("/medicalImagingList")
    public ApiResult<List<MedicalImaging>> list(@RequestBody SaleDataParam param) {

        LocalDateTime start = null;
        LocalDateTime end = null;
        // 获取当前日期（年月日）
        LocalDate now = LocalDate.now();
        if (Objects.equals("1", param.getSearchType())) {
            // 获取今天的开始时间（凌晨0点）
            start = LocalDateTime.of(now, LocalTime.MIN);
            // 获取今天的结束时间（晚上23点59分59秒）
            end = LocalDateTime.of(now, LocalTime.of(23, 59, 59));
        } else if (Objects.equals("2", param.getSearchType())) {
            // 获取本周的开始日期（周日或者周一作为一周的开始取决于你的定义）
            // 这里假设周一是一周的第一天
            LocalDate startOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 获取本周的结束日期（到周六的最后一刻）
            LocalDate endOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).minusDays(1);
            // 将开始和结束日期转换为LocalDateTime，并设定时间为00:00:00和23:59:59
            start = LocalDateTime.of(startOfWeek, LocalTime.MIN);
            end = LocalDateTime.of(endOfWeek, LocalTime.MAX);
        } else if (Objects.equals("3", param.getSearchType())) {
            // 获取本月的第一天（即月初）
            LocalDate startOfMonth = now.withDayOfMonth(1);
            // 获取下个月的第一天，并减去一天得到本月的最后一天（即月末）
            LocalDate endOfMonth = now.plusMonths(1).withDayOfMonth(1).minusDays(1);
            // 将开始和结束日期转换为LocalDateTime，并设定时间为00:00:00和23:59:59
            start = LocalDateTime.of(startOfMonth, LocalTime.MIN);
            end = LocalDateTime.of(endOfMonth, LocalTime.MAX);
        } else if (Objects.equals("4", param.getSearchType())) {
            // 获取本年的第一天（即年初）
            LocalDate startOfYear = now.withDayOfYear(1);
            // 获取下一年的第一天，并减去一天得到本年的最后一天（即年末）
            LocalDate endOfYear = now.plusYears(1).withDayOfYear(1).minusDays(1);
            // 将开始和结束日期转换为LocalDateTime，并设定时间为00:00:00和23:59:59
            start = LocalDateTime.of(startOfYear, LocalTime.MIN);
            end = LocalDateTime.of(endOfYear, LocalTime.MAX);
        }
        //根据时间区间查询
        LambdaQueryWrapper<MedicalImaging> lambdaQueryWrapper = new LambdaQueryWrapper<MedicalImaging>()
                .ge(MedicalImaging::getCreateTime, start)
                .le(MedicalImaging::getCreateTime, end);
        List<MedicalImaging> list = medicalImagingService.list(lambdaQueryWrapper);

        return success(list);

    }

    @OperationLog
    @ApiOperation("总销售额和支付笔数查询")
    @PostMapping("/payNumList")
    public ApiResult<PayNumListVO> payNumList() {
        PayNumListVO payNumListVO = new PayNumListVO();

        return success(payNumListVO);
    }

}
